package com.summer.io;

import org.junit.Test;

import java.io.*;

public class FileInputOutputStreamTest {
    @Test
    public void testFileInputStream()  {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File file = new File("com\\summer\\io\\messi.png");
            File destfile = new File("com\\summer\\io\\messi1.png");
           fis = new FileInputStream(file);
           fos = new FileOutputStream(destfile);
            byte[] cbyte = new byte[5];
            int len;
            while ((len=fis.read(cbyte))!=-1){
               fos.write(cbyte,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
           if (fis !=null) {
               try {
                   fis.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           if (fos != null){
               try {
                   fos.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }

    }
    @Test
    public void copyFile(String srcPath,String destPath)  {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File file = new File(srcPath);
            File destfile = new File(destPath);
            fis = new FileInputStream(file);
            fos = new FileOutputStream(destfile);
            byte[] cbyte = new byte[5];
            int len;
            while ((len=fis.read(cbyte))!=-1){
                fos.write(cbyte,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis !=null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
